package com.cloudxingkong.dianyinke.Welcome;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.cloudxingkong.dianyinke.NewTeacher.Ui.Login;
import com.cloudxingkong.dianyinke.R;

import java.util.ArrayList;
import java.util.List;

public class AndyViewPagerActivity extends AppCompatActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
	private double latitude;
	private double longitude;
	public AMapLocationClient mLocationClient = null;
	public AMapLocationClientOption option = null;
	private SharedPreferences preferences;

	private ViewPager vp;
	private WelcomeViewPagerAdapter vpAdapter;
	private List<View> views;
	private Button button;

	//引导图片资源
	private static final int[] pics = {R.drawable.a2, R.drawable.a3,
			R.drawable.a4};

	//底部小店图片
	private ImageView[] dots;

	//记录当前选中位置
	private int currentIndex;

	/** Called when the activity is first created. */
	@Override
	public void onCreate (Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView (R.layout.activity_welcome_page);

		button = (Button) findViewById (R.id.button);
		views = new ArrayList<View> ();

		LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams (LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);

		//初始化引导图片
		for (int i = 0; i < pics.length; i++) {
			ImageView iv = new ImageView (this);
			iv.setScaleType (ImageView.ScaleType.CENTER_CROP);
			iv.setLayoutParams (mParams);
			iv.setImageResource (pics[i]);
			views.add (iv);
		}
		vp = (ViewPager) findViewById (R.id.viewpager);
		//初始化Adapter
		vpAdapter = new WelcomeViewPagerAdapter (views);
		vp.setAdapter (vpAdapter);
		//绑定回调
		vp.setOnPageChangeListener (this);

		//初始化底部小圆点
		initDots ();
		button.setOnClickListener (new View.OnClickListener () {

			@Override
			public void onClick (View arg0) {
				Intent intent = new Intent ();
				intent.setClass (AndyViewPagerActivity.this, Login.class);
				AndyViewPagerActivity.this.startActivity (intent);
				finish ();
			}
		});

	}


	private void initDots () {
		LinearLayout ll = (LinearLayout) findViewById (R.id.ll);

		dots = new ImageView[pics.length];

		//循环取得小点图片
		for (int i = 0; i < pics.length; i++) {
			dots[i] = (ImageView) ll.getChildAt (i);
			dots[i].setEnabled (true);//都设为灰艿
			dots[i].setOnClickListener (this);
			dots[i].setTag (i);//设置位置tag，方便取出与当前位置对应
		}

		currentIndex = 0;
		dots[currentIndex].setEnabled (false);//设置为白色，即ꀤح状怿
	}

	/**
	 * 设置当前的引导页
	 */
	private void setCurView (int position) {
		if (position < 0 || position >= pics.length) {
			return;
		}

		vp.setCurrentItem (position);
	}

	/**
	 * 这只当前引导小点的ꀤؿ
	 */
	private void setCurDot (int positon) {
		if (positon < 0 || positon > pics.length - 1 || currentIndex == positon) {
			return;
		}

		dots[positon].setEnabled (false);
		dots[currentIndex].setEnabled (true);

		currentIndex = positon;
	}

	//当滑动状态改变时调用
	@Override
	public void onPageScrollStateChanged (int arg0) {
		// TODO Auto-generated method stub

	}

	//当当前页面被滑动时调甿
	@Override
	public void onPageScrolled (int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	//当新的页面被选中时调甿
	@Override
	public void onPageSelected (int arg0) {
		//设置底部小点选中状濠
		setCurDot (arg0);
		if (arg0 == 2) {
			button.setVisibility (View.VISIBLE);

		} else {
			button.setVisibility (View.GONE);
		}
	}

	@Override
	public void onClick (View v) {
		int position = (Integer) v.getTag ();
		setCurView (position);
		setCurDot (position);
	}

}
